<template>
	<section>
		<!--高级查询 +工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form  :inline="true">
				<el-form-item>
					<el-input v-model="keyword" placeholder="关键字" ></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="warning" v-on:click="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="addHandler">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

    <!--列表v-loading="listLoading"-->
    <el-table border :data="tableDatas" v-loading="listLoading" @selection-change="handleSelectionChange"
              highlight-current-row :row-key="getKey" ref="multable" style="width: 100%;">
      <!--多选框-->
      <el-table-column type="selection" width="55"reserve-selection>
      </el-table-column>
      <el-table-column type="index" width="70" label="序号" fixed="left">
      </el-table-column>
      <el-table-column prop="paramKey" label="参数键" width="160" fixed="left">
      </el-table-column>
      <el-table-column prop="paramDesc" label="参数描述" width="260" fixed="left">
      </el-table-column>
      <el-table-column prop="paramValue" label="参数值" width="430" fixed="left">
      </el-table-column>
<!--      <el-table-column prop="state" label="选择类型" width="100" >-->
<!--        &lt;!&ndash;是否置顶(0:文本;1:图片)&ndash;&gt;-->
<!--        <template scope="scope" >-->
<!--          <span v-if="scope.row.state==0"  style="color: #13ce66">文本</span>-->
<!--          <span v-if="scope.row.state==1"  style="color: red">图片</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column prop="updateUserName" label="修改人" width="100">
      </el-table-column>
      <el-table-column prop="updateTime" label="修改时间" width="170" sortable>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right">
        <template scope="scope">
          <el-button size="small"  @click="edit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

		<!-- 分页的大学   工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" :disabled="this.sels.length===0" @click="delBatch">批量删除</el-button>
			<el-pagination
					@size-change="handleSizeChange"
					@current-change="handleCurrentChange"
					:current-page="currentPage"
					:page-sizes="[5, 10]"
					:page-size="pageSize"
          :total="totals"
					layout="total, sizes, prev, pager, next, jumper"
          background
					style="float:right;">
			</el-pagination>
		</el-col>

    <!--
      :model="saveForm"  - 表单绑定模型数据对象saveForm
      label-width="80px"  - 标签的宽度
      :rules="saveFormRules" - 验证规则 - 表单项校验
      ref="saveForm" - 为当前对话框取名saveForm - 做表单校验

      label="姓名" - 标签名字
      prop="name" - 要做校验的时候必须要，不做校验就没有必要
      v-model="saveForm.name" - 双向绑定
      auto-complete="off" - 自动完成 - 关闭
    -->
    <!--新增界面-->
    <el-dialog :title="title" :visible.sync="saveFormVisible"  :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
<!--        <el-form-item label="修改人" prop="updateUserName">-->
<!--          <el-input v-model="saveForm.updateUserName" auto-complete="off"></el-input>-->
<!--        </el-form-item>-->

        <el-form-item label="参数键" prop="paramKey">
          <el-input v-model="saveForm.paramKey" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="state" >
          <el-radio-group v-model="saveForm.state" >
            <el-radio :label="0" border >文本</el-radio>
            <el-radio :label="1" border >图片</el-radio>
          </el-radio-group>
        </el-form-item>
<!--        <el-form-item label="参数值" prop="paramValue">-->
<!--          <el-input v-model="saveForm.paramValue" auto-complete="off"></el-input>-->
<!--        </el-form-item>-->
        <!--文件上传-->
        <el-form-item label="参数值"  v-if="saveForm.state==1">
          <el-upload class="upload-demo"
                     action="http://localhost:8080/minio"
                     name="file"
                     :on-remove="handleRemove"
                     :on-success="handleSuccess"
                     :on-exceed="exceed"
                     :file-list="fileList"
                     :limit=1
                     accept=".jpg,.jpeg,.png,.gif"
                     list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip" style="color: red">只能上传指定类型的图片 </div>
          </el-upload>
        </el-form-item>

        <el-form-item label="参数值" prop="paramValue" v-if="saveForm.state==0">
          <el-input v-model="saveForm.paramValue" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="参数描述" prop="paramDesc">
          <el-input v-model="saveForm.paramDesc" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" >提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>

	export default {
		data() {
			return {
			  //1.分页的数据
        tableDatas:[], //当前页数据
        listLoading:false,
        currentPage:1,//当前页
        pageSize:5,//每页显示的条数
        totals:0,//总条数

        //2.高级查询
        keyword:'',

        //3.当前行的数据   取id
        row:"",

        //4.批量删除选择
				sels: [], //存储多选记录

        //新增修改的对话框
        saveFormVisible:false,
        title:'',

        //7.上传的文件列表
        fileList: [],//上传的文件列表
        imgPrefix: "http://115.159.88.63:9090/java1125-test/",

				saveForm:{
          id:'',
          updateUserName:'',
          updateTime:'',//默认可用
          paramKey:'',
          paramValue:'',
          paramDesc:'',
          state:0
				},
        //表单项验证
        saveFormRules: {
          paramDesc: [
            {required: true, message: '请输入参数描述', trigger: 'blur'}
          ],
          paramKey: [
            {required: true, message: '请选择参数键', trigger: 'blur'}
          ],
          state: [
            {required: true, message: '', trigger: 'blur'}
          ],
          paramValue: [
            {required: true, message: '请选择参数键', trigger: 'blur'}
          ]

        },

			}
		},
		methods: {
      emptyPicture(){
        alert("11111111")
      },

      getKey(row){//跨页删除
        return row.id;},
      //高级查询----第几页时触发
      keywordQuery() {
        this.currentPage=1;//重置第一页
        this.getTableData();
      },
      //选择第几页时触发
      handleCurrentChange(curentPage) {
        this.currentPage = curentPage;
        this.getTableData();
      },
      //选择每页显示记录条数
      handleSizeChange(pageSize) {
        this.pageSize = pageSize;
        this.getTableData();
      },

    //----------------------------------------------
      //1.获取列表数据
      getTableData(){
        let para = {
          "currentPage": this.currentPage,
          "pageSize": this.pageSize,
          "keyword":this.keyword
        };
        this.listLoading = true; //显示加载圈
        //分页查询
        this.$http.post("/param", para).then(result=>{
          this.totals = result.data.total;//总条数
          this.tableDatas = result.data.list;//模型数据赋值
          this.listLoading = false;  //关闭加载圈
        });
      },

      //2.删除单条数据
      del(row){

        this.$confirm('确定删除此条数据吗？', '请确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.delete("/param/" + row.id).then(result=>{
            if(result.data.success){
              this.$message.success("删除成功");//成功
              //---------------------------------------
              //删除成功之后对当前页的处理【特殊情况：删除最后一页的最后一条数据】
              var totalPage = Math.ceil(this.totals/this.pageSize); //总页数 = 最后一页
              //如果当前页是最后一页                  并且是最后一条数据                  并且不是第一页
              if(this.currentPage==totalPage && (this.totals-1)%this.pageSize==0 && this.currentPage!=1){
                this.currentPage--;
              }

              this.getTableData();
            }else{
              this.$message.error("系统繁忙【500】");
            }
          }).catch(res=>{
            this.$message.error("系统繁忙【400,404】");
          });
        }).catch(() => {
          this.$message.info("已取消操作")
        });
      },

      //3.表格多条数据选中事件
      handleSelectionChange(val) {
        /**
         * val中存放的是所有已选择的行数据集合
         */
        this.sels = val.map(res => res.id);
        console.log(this.sels)
      },


      //3.批量删除
      delBatch(){
        this.$confirm('确定删除这些数据吗？', '请确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.patch("/param", this.sels).then(result=>{
            if(result.data.success){
              //答应后端的成功信息
              this.$message.success(result.data.message);//成功
              this.currentPage=1;//重置第一页
              this.getTableData();
            }else{
              this.$message.error("系统繁忙【500】");
            }
          }).catch(res=>{
            this.$message.error("系统繁忙【400,404】");
          });
        }).catch(() => {
          this.$message.info("已取消操作")
        });
      },

      //4.添加数据----------新增
      addHandler(){
        this.title="新增";
        this.saveForm = {
          id:'',
          updateUserName:'',
          updateTime:'',//默认可用
          paramKey:'',
          paramValue:'',
          paramDesc:'',
          state:0
        };

        this.fileList=[];//添加是把文件制空
        this.saveFormVisible = true;//显示对话框
      },
      //4.点击编辑 ---修改
      edit(row){
        this.title="修改";
        this.saveFormVisible = true;//显示编辑框
        this.saveForm = {
          id:'',
          updateUserName:'',
          updateTime:'',//默认可用
          paramKey:'',
          paramValue:'',
          paramDesc:'',
          state:''
        };
        this.saveForm = Object.assign({}, row);//回显数据

        //7.文件上传的回显删除
        this.fileList=[];
        if (row.paramValue){
          this.fileList.push({"url":row.paramValue});
        }

      },

			//6.提交新增表单数据
      saveSubmit(){
        this.$http.put("/param", this.saveForm).then(result=>{
          this.saveFormVisible = false;
					if(result.data.success){
            //答应后端的成功信息
            this.$message.success(result.data.message);//成功
            this.getTableData();
          }else{
            //this.$message.error("系统繁忙【500】");
            //打印后端的错误信息
            this.$message.error(result.data.message);
          }
				}).catch(res=>{
          this.$message.error("系统繁忙【400,404】");
        });
			},

      //7.文件删除的业务逻辑
      handleRemove(file, fileList) {
        var fileName = null;
        if(file.response){ //删除刚上传的图片
          fileName = file.response.resultObj;
        }else{//删除回显的图片
          fileName = file.url.substring(file.url.lastIndexOf("/")+1);
        }
        this.$http.delete("/minio/"+fileName).then(res=>{
          if(res.data.success){
            this.$message.success('删除成功');
            //文件删除了 也要制空 表单的文件
            this.fileList=[];
            this.saveForm.paramValue="";
          }else{
            this.$message.error('删除失败');
          }
        })
      },
      //8.文件上传的业务逻辑
      handleSuccess(response, file, fileList){
        this.saveForm.paramValue = this.imgPrefix + response.resultObj;
      },

      //8.文件超出个数限制时的钩子
      exceed(files, fileList){
        this.$message({
          message: "最多只能上传一张封面照哦",
          type: 'error'
        });
      },




		},
		mounted() {
		    this.getTableData();
		}
	}

</script>

<style scoped>

</style>
